<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员名称" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入会员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现编号" prop="withdrawSn">
        <el-input
          v-model="queryParams.withdrawSn"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现状态" prop="withdrawState">
        <el-select v-model="queryParams.withdrawState" placeholder="请选择提现状态" clearable>
          <el-option
            v-for="dict in dict.type.uac_member_withdraw_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditState">
        <el-select v-model="queryParams.auditState" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.uac_member_withdraw_audit_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人员" prop="auditorId">
        <el-input
          v-model="queryParams.auditorId"
          placeholder="请输入审核人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款状态" prop="payState">
        <el-select v-model="queryParams.payState" placeholder="请选择付款状态" clearable>
          <el-option
            v-for="dict in dict.type.uac_member_withdraw_pay_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payMethod">
        <el-select v-model="queryParams.payMethod" placeholder="请选择支付方式" clearable>
          <el-option
            v-for="dict in dict.type.uac_member_withdraw_pay_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['member:memberWithdraw:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withdrawList" @selection-change="handleSelectionChange">
      <el-table-column label="会员名称" align="center" prop="memberName"/>
      <el-table-column label="编号" align="center" prop="withdrawSn"/>
      <el-table-column label="提现金额" align="center" prop="withdrawAmount"/>
      <el-table-column label="实际金额" align="center" prop="realAmount"/>
      <el-table-column label="手续费" align="center" prop="fee"/>

      <el-table-column label="审核状态" align="center" prop="auditState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.uac_member_withdraw_audit_state" :value="scope.row.auditState"/>
        </template>
      </el-table-column>
      <el-table-column label="提现状态" align="center" prop="withdrawState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.uac_member_withdraw_state" :value="scope.row.withdrawState"/>
        </template>
      </el-table-column>
      <el-table-column label="付款状态" align="center" prop="payState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.uac_member_withdraw_pay_state" :value="scope.row.payState"/>
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="auditRemark"/>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditor"/>
      <el-table-column label="付款备注" align="center" prop="payRemark"/>
      <el-table-column label="支付方式" align="center" prop="payMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.uac_member_withdraw_pay_method" :value="scope.row.payMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleAudit(scope.row)"
            v-if="scope.row.auditState == 0"
            v-hasPermi="['member:memberWithdraw:audit']"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-if="canPay(scope.row)"
            @click="handleManualPay(scope.row)"
            v-hasPermi="['member:memberWithdraw:pay']"
          >线下打款
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 提现审核   -->
    <el-dialog title="提现审核" :visible.sync="auditOpen" v-if="auditOpen" width="500px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="80px">
        <el-form-item label="审核结果" prop="auditState">
          <el-select v-model="auditForm.auditState" placeholder="请选择审核结果">
            <el-option
              v-for="dict in dict.type.uac_member_withdraw_audit_state"
              :key="dict.value"
              :label="dict.label"
              :disabled="dict.value == 0"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input v-model="auditForm.auditRemark" placeholder="请输入审核备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAuditForm">确 定</el-button>
        <el-button @click="auditOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 人工打款   -->
    <el-dialog title="线下付款" :visible.sync="manualPayOpen" v-if="manualPayOpen" width="60%" append-to-body>
      <el-form ref="manualPayForm" :model="manualPayForm" :rules="manualPayRules" label-width="80px">
        <el-form-item label="付款凭据" >
          <el-upload
            :action="url"
            :headers="headers"
            list-type="picture-card"
            :limit=2
            :file-list="fileList"
            :on-success="handleReceiptsSuccess"
            :before-upload="beforeReceiptsUpload">
            <i  class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="付款备注" prop="payRemark">
          <el-input type="textarea" v-model="manualPayForm.payRemark" placeholder="请输入付款备注" maxlength="120" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitManualPayForm">确定打款</el-button>
        <el-button @click="manualPayOpen = false">取 消</el-button>
      </div>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>注意</span>
        </div>
        <p>1、为防止退款交易纠纷，打款凭证必传</p>
        <p>2、打款操作不可逆，请确保无误后再执行【确定打款】操作</p>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import {listWithdraw, getWithdraw,  withdrawManualPay, updateWithdraw, auditWithdraw} from "@/api/uac/withdraw";
import {mapGetters} from "vuex";

export default {
  name: "Withdraw",
  dicts: ['uac_member_withdraw_pay_state', 'uac_member_withdraw_audit_state', 'uac_member_withdraw_state','uac_member_withdraw_pay_method'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员提现表格数据
      withdrawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      auditOpen: false,
      manualPayOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        memberName: null,
        withdrawSn: null,
        withdrawAmount: null,
        realAmount: null,
        fee: null,
        withdrawState: null,
        auditState: null,
        auditRemark: null,
        auditTime: null,
        auditor: null,
        auditorId: null,
        payState: null,
        payRemark: null,
        payMethod: null,
        receipts: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      auditRules: {
        auditState: [
          {required: true, message: "请选择审核结果", trigger: "blur"},
        ],
        auditRemark: [
          {required: true, message: "请填写审核意见", trigger: "blur"},
        ]
      },
      // 审核表单
      auditForm: {
        id: null,
        auditState: null,
        auditRemark: ""
      },
      // 线下打款表单
      manualPayForm: {
        id: null,
        receipts: null,
        payRemark: ""
      },
      manualPayRules: {
      },
      url:process.env.VUE_APP_BASE_API + '/common/oss/upload',
      fileList: []
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters(['token']),
    headers() {
      return{
        "Authorization": this.token // 直接从本地获取token就行
      }
    }
  },
  methods: {
    /** 查询会员提现列表 */
    getList() {
      this.loading = true;
      listWithdraw(this.queryParams).then(response => {
        this.withdrawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        memberName: null,
        withdrawSn: null,
        withdrawAmount: null,
        realAmount: null,
        fee: null,
        withdrawState: null,
        auditState: null,
        auditRemark: null,
        auditTime: null,
        auditor: null,
        auditorId: null,
        payState: null,
        payRemark: null,
        payMethod: null,
        receipts: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员提现";
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      const id = row.id || this.ids
      this.auditOpen = true;
      this.auditForm.id = id
    },
    /** 线下打款按钮操作 */
    handleManualPay(row) {
      const id = row.id || this.ids
      this.manualPayOpen = true;
      this.manualPayForm.id = id
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWithdraw(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWithdraw(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审核提交按钮 */
    submitAuditForm() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          auditWithdraw(this.auditForm).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 人工打款提交按钮 */
    submitManualPayForm() {
      this.$refs["manualPayForm"].validate(valid => {
        let len = this.fileList.length;
        if (len < 1) {
          this.$modal.msgError("请上传付款凭据");
          return;
        }
        let receipts = [];
        for (let i = 0; i< len; i++){
          let item = this.fileList[i];
          receipts.push(item.url)
        }
        this.manualPayForm.receipts = receipts.join(",")
        if (valid) {
          withdrawManualPay(this.manualPayForm).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.manualPayOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('member/withdraw/export', {
        ...this.queryParams
      }, `withdraw_${new Date().getTime()}.xlsx`)
    },
    /** 校验是否能够显示打款按钮 */
    canPay(row){
      return row.auditState == 1 && (row.payState == 0 || row.payState == -1) && row.withdrawState  != 2
    },
    /** 打款凭据上传成功事件*/
    handleReceiptsSuccess(res, file) {
      let item = {"url": res.data.url}
      this.fileList.push(item);
    },
    beforeReceiptsUpload(file) {
      const isType = file.type === 'image/jpeg' || file.type === 'image/png'  || file.type === 'image/jpg';
      const isLt = file.size / 1024 / 1024 < 4;
      if (!isType) {
        this.$message.error('图片只能是 JPG、PNG 格式!!');
      }
      if (!isLt) {
        this.$message.error('上传图片大小不能超过4M!');
      }
      return isType && isLt;
    },
  }
};
</script>

<style lang="less" scoped>


</style>
